package com.tfswx.jcyw.dmzc.component;

import cn.hutool.core.bean.BeanUtil;
import com.intellij.ui.border.CustomLineBorder;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.InputMethodEvent;
import java.awt.event.InputMethodListener;

/**
 * @author 张述江
 * @date 2023/5/31
 */
public class MyTextArea extends JTextArea {

    public MyTextArea(Object obj, String zdmc) {
        super();

        setEnabled(true);
        setColumns(40);
        setRows(3);
        setLineWrap(true);
        setBorder(new CustomLineBorder(new Color(100, 100, 100), 1, 1, 1, 1));
        setBackground(new Color(0, 0, 0, 0));
        setOpaque(false);

        setText(BeanUtil.getProperty(obj, zdmc));


        getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                BeanUtil.setProperty(obj, zdmc, getText());
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                BeanUtil.setProperty(obj, zdmc, getText());
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                BeanUtil.setProperty(obj, zdmc, getText());
            }
        });

        addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                setBorder(new BevelBorder(BevelBorder.LOWERED, new Color(100, 100, 100), new Color(61, 97, 133),
                        new Color(100, 100, 100), new Color(61, 97, 133)));
            }

            @Override
            public void focusLost(FocusEvent e) {
                setBorder(new CustomLineBorder(new Color(100, 100, 100), 1, 1, 1, 1));
            }
        });
    }


}
